package com.eastday.biz.impl;

import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.eastday.biz.ParseXmlConfigBiz;
import com.eastday.model.SysConfigModel;
import com.eastday.model.UrlConfigModel;

public class ParseXmlConfigBizImpl implements ParseXmlConfigBiz {

	@Override
	public SysConfigModel parseSysConfig() throws ParserConfigurationException, SAXException, IOException {
		// TODO Auto-generated method stub
		SysConfigModel sysConfigModel = new SysConfigModel();
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		Document doc = documentBuilder.parse("SysConfig.xml");
		sysConfigModel.setHostip(doc.getElementsByTagName("hostip").item(0).getFirstChild().getNodeValue());
		sysConfigModel.setHostport(doc.getElementsByTagName("hostport").item(0).getFirstChild().getNodeValue());
		sysConfigModel.setDbname(doc.getElementsByTagName("dbname").item(0).getFirstChild().getNodeValue());
		sysConfigModel.setUsername(doc.getElementsByTagName("username").item(0).getFirstChild().getNodeValue());
		sysConfigModel.setPassword(doc.getElementsByTagName("password").item(0).getFirstChild().getNodeValue());
		sysConfigModel.setDivid(doc.getElementsByTagName("divid").item(0).getFirstChild().getNodeValue());
		sysConfigModel.setInterval(Integer.valueOf(doc.getElementsByTagName("interval").item(0).getFirstChild().getNodeValue()));
		sysConfigModel.setTemppath(doc.getElementsByTagName("temppath").item(0).getFirstChild().getNodeValue());
		sysConfigModel.setExportPageSize(Integer.valueOf(doc.getElementsByTagName("exportpagesize").item(0).getFirstChild().getNodeValue()));
		sysConfigModel.setExportPath(doc.getElementsByTagName("exportpath").item(0).getFirstChild().getNodeValue());
		sysConfigModel.setExportFilename(doc.getElementsByTagName("exportfilename").item(0).getFirstChild().getNodeValue());
		sysConfigModel.setLogfile(doc.getElementsByTagName("logfile").item(0).getFirstChild().getNodeValue());
		return sysConfigModel;
	}

	@Override
	public List<UrlConfigModel> parseUrlConfig() throws ParserConfigurationException, SAXException, IOException {
		// TODO Auto-generated method stub
		List<UrlConfigModel> urlConfigModelArray = new ArrayList<UrlConfigModel>();
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		Document doc = documentBuilder.parse("UrlConfig.xml");
		NodeList urlArray = doc.getElementsByTagName("url");
		for(int i=0;i<urlArray.getLength();i++){
			UrlConfigModel urlConfigModel = new UrlConfigModel();
			Node urlNode = urlArray.item(i);
			urlConfigModel.setUrl(urlNode.getAttributes().item(0).getFirstChild().getNodeValue());
			NodeList urlTextArray = urlNode.getChildNodes();
			
			for(int j=0;j<urlTextArray.getLength();j++){
				if(urlTextArray.item(j).getNodeType()==Node.ELEMENT_NODE){
					if(urlTextArray.item(j).getNodeName().equals("channel")){
						if(urlTextArray.item(j).getFirstChild()!=null){
							urlConfigModel.setChannel(urlTextArray.item(j).getFirstChild().getNodeValue());
						}
						else{
							urlConfigModel.setChannel("");
						}
					}
					else if(urlTextArray.item(j).getNodeName().equals("filename")){
						if(urlTextArray.item(j).getFirstChild()!=null){
							urlConfigModel.setFilename(urlTextArray.item(j).getFirstChild().getNodeValue());
						}
						else{
							urlConfigModel.setFilename("");
						}
					}
					else if(urlTextArray.item(j).getNodeName().equals("prefix")){
						if(urlTextArray.item(j).getFirstChild()!=null){
							urlConfigModel.setPrefix(urlTextArray.item(j).getFirstChild().getNodeValue());
						}
						else{
							urlConfigModel.setPrefix("");
						}
					}
					else{
						break;
					}
				}
			}
			urlConfigModelArray.add(urlConfigModel);
		}
		return urlConfigModelArray;
	}

}
